package com.blb.community_admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.ZyRepair;
import com.blb.community_admin.entity.vo.RepairVO;
import com.blb.community_admin.entity.vo.ZyRepairVO;
import com.blb.community_admin.service.ZyRepairService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@CrossOrigin
@RestController
@RequestMapping("/repair")
@Api(tags = "报修模块")
public class ZyRepairController {

    @Autowired
    ZyRepairService zyRepairService;

    @ApiOperation(value = "模糊查询分页", notes = "模糊查询+分页")
    @PostMapping("ZyRepair/pageRepair")
    public Map<Object, Object> selectRepair(@RequestBody RepairVO zyRepairVO) {

        HashMap<Object, Object> map = new HashMap<>();

        List<ZyRepair>  zyRepairList  = zyRepairService.selectpageRepair(zyRepairVO);

        int total = zyRepairList.size();

        map.put("zyRepairList", zyRepairList);
        map.put("total", total);
        return map;
    }

    @Log(module = "报修模块", operationType = 1, requestMethod = "POST")
    @ApiOperation(value = "添加报修信息", notes = "用于添加报修信息")
    @PostMapping("ZyRepair/addRepair")
    public ResponseResult<String> insertZyRepair(@RequestBody ZyRepair zyRepair ) {

        String format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        zyRepair.setReceivingOrdersTime(format);
        zyRepairService.save(zyRepair);
        return ResponseResult.ok("添加成功");
    }

    @Log(module = "报修模块", operationType = 2, requestMethod = "PUT")
    @ApiOperation(value = "修改报修信息", notes = "用于修改报修信息")
    @PutMapping("ZyRepair/updateRepair")
    public  ResponseResult<String> updateRepair(@RequestBody ZyRepair zyRepair) {
        //派单时间repair_state
        if (zyRepair.getRepairState().equals("Allocated")){
            String format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            zyRepair.setAssignmentTime(format);
        }
        //已处理时间complete_time
        if (zyRepair.getRepairState().equals("Processed")){
            String format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            zyRepair.setCompleteTime(format);
        }
        //已取消cancel_time
        if (zyRepair.getRepairState().equals("Cancelled")){
            String format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
            zyRepair.setCancelTime(format);
        }
        zyRepairService.updateById(zyRepair);

        return ResponseResult.ok("修改成功");


    }

    @Log(module = "报修模块", operationType = 2, requestMethod = "PUT")
    @ApiOperation(value = "修改报修状态", notes = "用于修改报修状态")
    @PostMapping("ZyRepair/updateRepairState")
    public ResponseResult<String> updateRepairState(@RequestBody ZyRepair zyRepair) {
        ZyRepair newzyRepair = new ZyRepair();
        if (zyRepair.getRepairState().isEmpty()){
            newzyRepair.setRepairState(zyRepair.getRepairState());
            newzyRepair.setRepairId(zyRepair.getRepairId());
        }
        zyRepairService.updateById(newzyRepair);
        return ResponseResult.ok("修改报修状态成功");

    }

    @Log(module = "报修模块", operationType = 3, requestMethod = "DELETE")
    @ApiOperation(value = "删除", notes = "用于删除报修信息")
    @DeleteMapping("ZyRepair/deleteZyRepairById")
    public ResponseResult<String> deleteZyRepairById(@RequestParam(name = "ownerId") String repairId) {
        zyRepairService.removeById(repairId);
        return ResponseResult.ok("删除成功");
    }

    @Log(module = "报修模块", operationType = 5, requestMethod = "POST")
    @ApiOperation(value = "报修信息导出", notes = "报修信息导出")
    @PostMapping("ZyRepair/getExcel")
    public ResponseEntity<ByteArrayResource> simpleWritea(@RequestBody ArrayList<String> repairIds, HttpServletResponse response ) {
        ResponseEntity<ByteArrayResource> download = null;
        try {
            List<ZyRepair> zyRepairList = null;
            if (repairIds.isEmpty()) {
                zyRepairList = zyRepairService.selectRepairAll();
            } else {
                zyRepairList = zyRepairService.selectRepairByIds(repairIds);
            }
            String path = EasyExcelUtil.write("报修表数据", ZyRepair.class, zyRepairList);
            download = EasyExcelUtil.download(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return download;
    }

//    @ApiOperation("查询报修列表数据")
//    @GetMapping("/repair/getRepairs")
//    public ResponseResult<IPage<ZyRepairVO>> getRepairs(Long current){
//        IPage<ZyRepairVO> repairs = zyRepairService.getRepairs(current, 5L);
//        return ResponseResult.ok(repairs);
//    }

    @ApiOperation("查询报修列表数据")
    @GetMapping("/repair/getRepairs/getRepairsByTerm")
    public ResponseResult<IPage<ZyRepairVO>> getRepairs(Long current,String repairState,String ownerRealName,String ownerPhoneNumber,String communityId){
        System.out.println(repairState+ownerPhoneNumber+ownerRealName+communityId);
        IPage<ZyRepairVO> repairs = zyRepairService.getRepairs(current, 5L,repairState,ownerRealName,ownerPhoneNumber,communityId);
        return ResponseResult.ok(repairs);
    }




}
